using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Xivo
{
  public partial class EditScheduleItemForm : Form
  {
    public delegate void ItemSavedEvent(object sender, ScheduleItem e);
    public event ItemSavedEvent ItemSaved;

    private ScheduleItem _item = null;
    public ScheduleItem Item
    {
      get { return _item; }
      set
      {
        //SaveItem(_item);
        _item = value;
        LoadItem(_item);
      }
    }

    void SaveItem(ScheduleItem item)
    {
      if (item == null)
        return;
      item.Channel = int.Parse(tbChannel.Text);
      item.DaysOfWeek[0] = cbSun.Checked;
      item.DaysOfWeek[1] = cbMon.Checked;
      item.DaysOfWeek[2] = cbTues.Checked;
      item.DaysOfWeek[3] = cbWed.Checked;
      item.DaysOfWeek[4] = cbThu.Checked;
      item.DaysOfWeek[5] = cbFri.Checked;
      item.DaysOfWeek[6] = cbSat.Checked;
      item.Duration = int.Parse(tbDuration.Text);
      item.Enabled = cbEnabled.Checked;
      item.Name = tbName.Text;
      item.RepeatType = (ScheduleItem.RepeatTypeEnum)  cRepeat.SelectedItem;
      item.StartTime = dateTimePicker1.Value;
    }

    void LoadItem(ScheduleItem item)
    {
      if (_item == null)
        return;
      tbChannel.Text = item.Channel.ToString();
      cbSun.Checked  = item.DaysOfWeek[0];
      cbMon.Checked  = item.DaysOfWeek[1];
      cbTues.Checked = item.DaysOfWeek[2];
      cbWed.Checked  = item.DaysOfWeek[3];
      cbThu.Checked  = item.DaysOfWeek[4];
      cbFri.Checked  = item.DaysOfWeek[5];
      cbSat.Checked  = item.DaysOfWeek[6];
      tbDuration.Text = item.Duration.ToString();
      cbEnabled.Checked = item.Enabled;
      tbName.Text = item.Name;
      cRepeat.SelectedItem = item.RepeatType;
      dateTimePicker1.Value = item.StartTime;
    }

    public EditScheduleItemForm()
    {
      InitializeComponent();
    }

    private void EditScheduleItemForm_Load(object sender, EventArgs e)
    {

    }

    private void bSave_Click(object sender, EventArgs e)
    {
      try
      {

        SaveItem(Item);
        if (ItemSaved != null)
        {
          ItemSaved(this, Item);
        }
        Hide();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    private void bCancel_Click(object sender, EventArgs e)
    {
      Hide();
    }

    private void tbChannel_Validating(object sender, CancelEventArgs e)
    {
      TextBox t=(TextBox) sender;
      int newValue;
      if(! int.TryParse(t.Text,out newValue))
        t.BackColor=System.Drawing.Color.Red;
      else
        t.BackColor=System.Drawing.SystemColors.Window;
    }

    private void EditScheduleItemForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (e.CloseReason == CloseReason.UserClosing)
      {
        e.Cancel = true;
        Hide();
      }
    }
  }
}